package com.szxy.hotel.controller;

import com.szxy.hotel.entity.Hotelroom;
import com.szxy.hotel.entity.PageBean;
import com.szxy.hotel.entity.Room;
import com.szxy.hotel.service.RoomstatusService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RoomstatusController {
    @Resource(name = "roomstatusService")
    RoomstatusService roomstatusService;

    /**
     * 客房管理列表
     */
    @RequestMapping("/roomstatusList.do")
    @ResponseBody
    public Map<String,Object> roomstatusList(int currentPage, String eroom,String rstate,String rtype){
        Map<String, Object> map = new HashMap<String, Object>();
        PageBean<Hotelroom> pageBean = new PageBean<Hotelroom>();
        PageBean<Room> pageBean1 = new PageBean<Room>();
        int pageSize = 8;
        int start = (currentPage -1) * pageSize;
        int size = pageSize;
        if(eroom==""||eroom==null){
            if(rstate!=null&&rtype!=null){
                List<Room> lists = roomstatusService.findByPage2(start, size,rstate,rtype);
                List<Room> list = roomstatusService.selectCount2(rstate,rtype);
                int count = list.size();
                if (count <= 0) {
                    map.put("status", 1);
                    map.put("message", "没有数据");
                    map.put("result", null);
                }else {
                    int totalCount = count;
                    double tc = totalCount;
                    //总页数
                    int totalPage = (int) Math.ceil(tc / pageSize);
                    pageBean1.setCurrentage(currentPage);
                    pageBean1.setPageSize(pageSize);
                    pageBean1.setTotalCount(totalCount);
                    pageBean1.setTotalPage(totalPage);
                    pageBean1.setLists(lists);
                    map.put("status", 0);
                    map.put("message", "有数据");
                    map.put("result", pageBean1);
                }
            }else {
                String roomState1 = "已预订";
                String roomState2 = "已入住";
                List<Room> lists = roomstatusService.findByPage1(start, size,roomState1,roomState2);
                List<Room> list = roomstatusService.selectCount1(roomState1,roomState2);
                int count = list.size();
                if (count <= 0) {
                    map.put("status", 1);
                    map.put("message", "没有数据");
                    map.put("result", null);
                }else {
                    int totalCount = count;
                    double tc = totalCount;
                    //总页数
                    int totalPage = (int) Math.ceil(tc / pageSize);
                    pageBean1.setCurrentage(currentPage);
                    pageBean1.setPageSize(pageSize);
                    pageBean1.setTotalCount(totalCount);
                    pageBean1.setTotalPage(totalPage);
                    pageBean1.setLists(lists);
                    map.put("status", 0);
                    map.put("message", "有数据");
                    map.put("result", pageBean1);
                }
            }
        }else {
            String hotelState1 = "已预订";
            String hotelState2 = "已入住";
            List<Hotelroom> lists = roomstatusService.findByPage(start, size,hotelState1,hotelState2);
            List<Hotelroom> list = roomstatusService.selectCount(hotelState1,hotelState2);
            int count = list.size();
            if (count <= 0) {
                map.put("status", 1);
                map.put("message", "没有数据");
                map.put("result", null);
            }else {
                int totalCount = count;
                double tc = totalCount;
                //总页数
                int totalPage = (int) Math.ceil(tc / pageSize);
                pageBean.setCurrentage(currentPage);
                pageBean.setPageSize(pageSize);
                pageBean.setTotalCount(totalCount);
                pageBean.setTotalPage(totalPage);
                pageBean.setLists(lists);
                map.put("status", 0);
                map.put("message", "有数据");
                map.put("result", pageBean);
            }
        }
        return map;
    }
}
